package bll

import (
	"context"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
)

// Create 创建数据
func (a *Video) CreateGroupVideo(ctx context.Context, item schema.Video) (*schema.IDResult, error) {

	item.ID = uuid.NewID()
	err := a.VideoModel.Create(ctx, item)
	if err != nil {
		return nil, err
	}

	if len(item.GroupId) > 0 && item.ContentType == 1 {
		err := a.GroupHomeModel.Create(ctx, schema.GroupHome{ID: uuid.NewID(), GroupId: item.GroupId, ContentId: item.ID, Creator: item.Creator})
		if err != nil {
			return nil, errors.New400Response("创建群首页语音失败：%s", err.Error())
		}

		if !a.VideoModel.IsCreateToday(ctx) {
			a.GroupMemberModel.AddReciteDay(ctx)
			a.UserInfoModel.AddReciteDay(ctx)
		}
	}

	if len(item.GroupId) > 0 && item.ContentType == 2 {
		err := a.GroupVideoModel.Create(ctx, schema.GroupVideo{ID: uuid.NewID(), GroupId: item.GroupId, ContentId: item.ID, Creator: item.Creator})
		if err != nil {
			return nil, errors.New400Response("创建群视频失败：%s", err.Error())
		}
	}

	return schema.NewIDResult(item.ID), nil
}
